package com.computerManagementSystem.controller;

import com.computerManagementSystem.service.MemberServiceImpl;
import com.computerManagementSystem.util.CookieUtil;
import com.computerManagementSystem.util.JWTUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 赖志宇
 * @date 2020/6/18 1:43
 */

@RequestMapping(value = "member",method = RequestMethod.GET)
@Controller
public class MemberPageController {
    @Resource(name = "MemberService")
    private MemberServiceImpl memberService;
    /**
     * 用户主页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("index")
    public ModelAndView IndexPage(HttpServletRequest request,HttpServletResponse response) {
        //加入权限判断，一共有四种不同的主页

        String role = JWTUtil.getClaimNamedRole(CookieUtil.getCookieValueNamedRoleToken(request));
        if(role==null){
            String token = CookieUtil.getCookieValueNamedToken(request);
            String id = JWTUtil.getClaimNamedAccount(token);
             role = memberService.getMemberRoleByID(id);
            String roleToken = JWTUtil.createRoleToken(role);
            CookieUtil.setRoleCookie(response,roleToken);
        }
        Map<String,String> roleMap = new HashMap<String,String>(){
            {
              put("普通队员","Member/MemberIndex");
              put("接线员","Operator/OperatorIndex");
              put("管理员","Admin/AdminIndex");
              put("超管","Admin/SuperAdminIndex");
            }
        };
        return new ModelAndView(roleMap.get(role));
    }

    /**
     * 用户登录页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("login")
    public ModelAndView loginPage(){
        return new ModelAndView("Member/MemberLogin");
    }

    /**
     * 用户个人信息页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("personalInfoPage")
    public ModelAndView personalInformationPage(){
        return new ModelAndView("Member/PersonalMessage");
    }

    /**
     * 用户查看个人任务页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("myTaskList")
    public ModelAndView memberTaskListPage(){
        return new ModelAndView("Member/MyTaskList");
    }

    /**
     * 用户修改密码页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("changePassword")
    public ModelAndView changePasswordPage(){
        return new ModelAndView("Member/ChangePassword");
    }

    @GetMapping("getWaitingReturnList")
    public ModelAndView returnThingPage(){
        return new ModelAndView("Member/WaitingReturnList");
    }

    @GetMapping("getCanBorrowList")
    public ModelAndView canBorrowPage(){
        return new ModelAndView("Member/CanBorrowList");
    }

    @GetMapping("quit")
    public String quit(HttpServletResponse response){
        CookieUtil.removeMemberCookie(response,"author_token");
        CookieUtil.removeMemberCookie(response,"role_token");
        return "redirect:login";
    }
}
